# 找自己电脑上的.doc后缀的word文档，调研相关的库和依赖，进行读写处理。
import os
import win32com.client as win32
from docx import Document

def find_doc_files(directory):
    doc_files = []
    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.doc'):
                doc_files.append(os.path.join(root, file))
    return doc_files

def convert_doc_to_docx(doc_path, docx_path):
    word = win32.Dispatch('Word.Application')
    word.Visible = False
    doc = word.Documents.Open(doc_path)
    doc.SaveAs(docx_path, FileFormat=16) 
    doc.Close()
    word.Quit()

def read_docx(docx_path):
    doc = Document(docx_path)
    full_text = []
    for para in doc.paragraphs:
        full_text.append(para.text)
    return '\n'.join(full_text)

def write_docx(docx_path, text):
    doc = Document()
    doc.add_paragraph(text)
    doc.save(docx_path)

def main():
    directory_to_search = '"C:\Users\lenovo\Desktop\word.docx"'
    doc_files = find_doc_files(directory_to_search)

    for doc_file in doc_files:
        docx_file = doc_file.rsplit('.', 1)[0] + '.docx'
        print(f'Converting {doc_file} to {docx_file}...')
        convert_doc_to_docx(doc_file, docx_file)
        print(f'Reading {docx_file}...')
        content = read_docx(docx_file)
        print(f'Content of {docx_file}:\n{content[:200]}...') # 显示200字符

if __name__ == '__main__':
    main()